const express = require('express')
const app = express()
// 导入中间件bodyParser
var bodyParser = require('body-parser')
// 当extended为false是值为数组当为ture是为任意值
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.use((req, res, next) => {
    // state等于1是失败
    res.cc = (err, state = 1) => {
        res.statusCode = 500
        res.send({
            state,
            message: err instanceof Error ? err.message : err
        })
    }
    next()
})
// 导入cors
const cors = require('cors')
// 全局挂在
app.use(cors())
// 导入multer用于图片传输
const multer = require('multer')
const limits = {
    fileSize: 102400 * 10240 // 限制文件大小为 1MB
}
const upload = multer({ dest: './public/upload', limits: limits })
app.use(upload.any())
app.use(express.static('./public'))

// 导入登录注册的
const login = require('./router/login')
app.use('/api', login)
// 导入车辆管理
const mycar = require('./router/mycar')
app.use('/car', mycar)
// 导入地址管理
const myaddress = require('./router/myaddress')
app.use('/address', myaddress)
// 导入机长代码
const numberList = require('./router/numbe')
app.use('/number', numberList)
// 导入门店
const store = require('./router/store')
app.use('/store', store)
// 导入预约
const subscribe = require('./router/subscribe')
app.use('/subscribe', subscribe)
// 导入图片识别
const recognitionImg = require('./router/recognitionImg')
app.use('/recognitionimg', recognitionImg)
// 支付
const play = require('./router/play')
app.use('/play', play)
// 导入用户
const adminUser = require('./router/adminUser')
app.use('/adminUser', adminUser)
// 导入车主
const carOwner = require('./router/carOwner')
app.use('/carOwner', carOwner)
// 资源管理
const resource = require('./router/resource')
app.use('/resource', resource)
// 报价管理
const carBrand = require('./router/carBrand')
app.use('/carBrand', carBrand)
// 发货管理
const quality = require('./router/quality')
app.use('/quality', quality)
// 车险管理
const insurance = require('./router/insurance')
app.use('/insurance', insurance)
// 周边管理
const shopping = require('./router/shpping')
app.use('/shopping', shopping)
// 获取城市
const addressOptions = require('./router/addressOptions')
app.use('/addressOptions', addressOptions)
// 订单
const order = require('./router/order')
app.use('/order', order)

// 导入jwt
const jwtconfig = require('./jwt_config/index')

const { expressjwt: jwt } = require('express-jwt')
app.use(jwt({
    secret: jwtconfig.jwtSecretKey, algorithms: ["HS256"]
}).unless({
    path: [/^\/api\//]
}))
// // 对不符合joi条件的进行报错
// app.use((req,res,next)=>{
//     if (err instanceof Joi.ValidationError) return res.cc(err)
//     next()
// })


app.use(express.static(__dirname + '/public'))
// 为端口号
const port = 3008
app.listen(port, () => {

    console.log('启动成功', port)
})